package com.ycxh;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.awt.*;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.URI;

@SpringBootApplication
@Slf4j
public class RemoteDeployApplication {

    public static String url = null;

    public static void main(String[] args) throws Exception {
        System.out.println("----RemoteDeployApplication1----");
        ConfigurableApplicationContext application = SpringApplication.run(RemoteDeployApplication.class, args);

        System.out.println("----RemoteDeployApplication2----");

        // 下面的代码会在CommandLineRunner.run之后执行
        // ----RemoteDeployApplication1----
        // ----MyCommandRunner----
        // ----RemoteDeployApplication2----

        // Springboot项目启动后自动打开浏览器访问(超实用)
        // https://blog.csdn.net/rongbo91/article/details/109668937
        Environment env = application.getEnvironment();
        Boolean openBrow = env.getProperty("openBrow", Boolean.class);
        if (!openBrow) {
            return;
        }

        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        if (StringUtils.isEmpty(path)) {
            path = "";
        }
        // url = "http://localhost:" + port + path;
        url = "http://127.0.0.1:" + port + path;
        log.info("\n----------------------------------------------------------\n\t" +
                "Application  is running! Access URLs:\n\t" +
                "Local访问网址: \t\t" + url + "\n\t" +
                "External访问网址: \thttp://" + ip + ":" + port + path + "\n\t" +
                "----------------------------------------------------------");
        String jvmName = ManagementFactory.getRuntimeMXBean().getName();
        log.info("当前项目进程号：" + jvmName.split("@")[0]);

        openBrowPop(url);
    }

    // 如何使用Java打开默认的Web浏览器
    // https://www.codenong.com/5226212/
    public static void openBrowPop(String url) throws Exception {
        System.out.println("----openBrowPop----");
        if (StringUtils.isNotBlank(url)) {
            String myOS = System.getProperty("os.name").toLowerCase();

            try {
                if (Desktop.isDesktopSupported()) { // Probably Windows
                    Desktop desktop = Desktop.getDesktop();
                    desktop.browse(new URI(url));

                } else {
                    Runtime runtime = Runtime.getRuntime();
                    if (myOS.contains("mac")) { // Apples
                        runtime.exec("open" + url);
                    } else if (myOS.contains("nix") || myOS.contains("nux")) { // Linux flavours
                        runtime.exec("xdg-open" + url);
                    } else {
                        runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
                    }

                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            System.out.println("----openBrowPop----url为空");
        }
    }

}
